﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

namespace WinCryptor2008
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.DoEvents();
            switch (args.Length)
            {
                case 1:
                {
                    MainForm form = new MainForm(true, GetLongName(args[0]));
                    if (form.IsOK)
                    {
                        Application.Run(form);
                    }
                } break;
                case 2:
                {
                    int arg1;
                    if (!Int32.TryParse(args[1], out arg1))
                    {
                        MessageBox.Show("Invalid input arguments: " + args[1] + " cannot be converted to an integer\r\n\r\nRequired Argument format: Cryptor.exe [<file_to_use> [<1 -> encrypting>]]", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    MainForm form = new MainForm(arg1 == 1, GetLongName(args[0]));
                    if (form.IsOK)
                    {
                        Application.Run(form);
                    }
                } break;
            }
        }

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint GetLongPathName(string lpszShortPath, StringBuilder lpszLongPath, uint cchBuffer);

        public static string GetLongName(string shortName)
        {
            StringBuilder longNameBuffer = new StringBuilder(256);
            uint bufferSize = (uint)longNameBuffer.Capacity;
            GetLongPathName(shortName, longNameBuffer, bufferSize);
            return longNameBuffer.ToString();
        }
    }
}
